

#include "unity.h"
#include "unity_fixture.h"

#include "Debug.h"
#include "BSP_Sleep.h"

#include <phApp_Init.h>
#include <Reader/Reader.h>
#include <Reader/ReaderISO14443.h>

#define LISTEN_PHASE_TIME_MS		10


TEST_GROUP(ISO14443);


TEST_SETUP(ISO14443)
{
	reader.hfFlags = TAG_MASK_MIFARE;
	Reader_NFC_LoadProfile(reader.hfFlags, 0, 0, 0);
}

TEST_TEAR_DOWN(ISO14443)
{
	Reader_NFC_RfOff();
}


TEST(ISO14443, PollA)
{
	SimpleTag_T simpleTag;
	int ret;
	uint8_t ats[64];
	uint8_t alen;
//		phacDiscLoop_Sw_DataParams_t *pDataParams = (phacDiscLoop_Sw_DataParams_t *)reader.pDiscLoop;
	while(1){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
 		ret = Reader_NFC_Discovery2(&simpleTag, 0);
		if(ret){
			continue;
		}
		ret = Reader_ISO14443A_GetATS(ats, &alen);
		if(ret){
			LOG_E("ats error!");
			continue;
		}
		LOG_B("ats", ats, alen, 16);
		break;
	}
}


TEST(ISO14443, TDX)
{
	SimpleTag_T simpleTag;
	int ret;
//		phacDiscLoop_Sw_DataParams_t *pDataParams = (phacDiscLoop_Sw_DataParams_t *)reader.pDiscLoop;
	while(1){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery2(&simpleTag, 0);
		if(ret){
			continue;
		}
		uint8_t ats[64];
		uint8_t alen;
		ret = Reader_ISO14443A_GetATS(ats, &alen);
		if(ret){
			LOG_E("ats error!");
			continue;
		}
		LOG_B("ats", ats, alen, 16);
		uint8_t capdu[] = {0x00, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00};
		uint8_t rapdu[64];
		uint16_t rlen;
		LOG_B("ats", capdu, sizeof(capdu), 16);
		ret = Reader_ISO14443_4_Exchange(capdu, sizeof(capdu), rapdu, &rlen, sizeof(rapdu));
		if(ret){
			LOG_E("apdu error!");
			continue;
		}
		LOG_B("rapdu", rapdu, rlen, 16);
	}
}


